<?php

namespace App\Http\Controllers\Estate;

use App\Http\Controllers\Controller;
use App\Models\Order;
use App\Services\MaintainService;
use Illuminate\Http\Request;

class MaintainController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @param MaintainService $maintain_service
     * @param Request $request
     * @return \Illuminate\Http\Response
     */
    public function index(MaintainService $maintain_service, Request $request)
    {
        $user  = $request->user();
        //查询当前物业自己的数据
        $where['e_id'] = $user['estate_id'];
        $rs = $maintain_service->getMaintains($where);
        return $rs['status'] ? $this->success($rs['data']) : $this->error($rs['msg']);
    }


    /**
     * Display the specified resource.
     *
     * @param MaintainService $maintain_service
     * @param int $id
     * @return \Illuminate\Http\Response
     */
    public function show(MaintainService $maintain_service, $id)
    {
        $rs = $maintain_service->getMaintainInfoById($id);
        return $rs['status'] ? $this->success($rs['data']) : $this->error($rs['msg']);
    }

    /**
     * Update the specified resource in storage.
     *
     * @param \Illuminate\Http\Request $request
     * @param int $id
     * @return \Illuminate\Http\Response
     */
    public function update(MaintainService $maintain_service, $id)
    {
        $data = request()->post();
        //修改投诉状态
        $res = $maintain_service->setStatus($id, $data);
        return $res['status'] ? $this->success($res['data']) : $this->error($res['msg']);
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param int $id
     * @return \Illuminate\Http\Response
     */
    public function destroy(Order $order_model, $id)
    {
        $idArray = array_filter(explode(',', $id), function ($item) {
            return is_numeric($item);
        });
        $order_model->destroy($idArray);
        return $this->success(__('base.success'));
    }
}
